﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web.UI;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectBestActivity : WebActivity
    {
        public static UIButton BSSArea(Subject sub, String model, bool isApp, bool IsNotComment)
        {
            var best = new UIEventText('\uf087', sub.Favs > 0 ? String.Format("({0})", sub.Favs) : "赞").Format("{icon}{text}")
           .Style(new UIStyle().Radius(20).BorderColor(0x999).Color(0x999).Name("icon", new UIStyle().Font("wdk")).Size(16));

            best.Click(Web.UIClick.Click(new UIClick() { Model = model, Command = "Best" }.Send("Key", sub.Id.ToString())));
            var footer = new UIButton();
            footer.Style.AlignCenter().Height(40).Padding(10, 20).Name("min-width", 80);//.Radius(20);
            if (sub.IsComment == false || IsNotComment)
            {
                if (isApp)
                {
                    footer.Button(best,
                        new UIEventText("分享").Style(new UIStyle().Radius(20).Color(0x999).BorderColor(0x999).Size(16))
                        .Click(Web.UIClick.Click(new UIClick(model, "Share", sub.Id.ToString()))));
                }
                else
                {
                    footer.Button(best);

                }
            }
            else
            {
                if (isApp)
                {
                    footer.Button(best, new UIEventText("评论").Style(new UIStyle().Radius(20).Color(0x999).BorderColor(0x999).Size(16))
                    .Click(Web.UIClick.Click(new UIClick() { Model = model, Command = "Comment" }.Send("Refer", sub.Id.ToString()))),
                    new UIEventText("分享").Style(new UIStyle().Radius(20).Color(0x999).BorderColor(0x999).Size(16))
                    .Click(Web.UIClick.Click(new UIClick(sub.Id.ToString()) { Model = model, Command = "Share" })));
                }
                else
                {

                    footer.Button(best, new UIEventText("评论").Style(new UIStyle().Radius(20).Color(0x999).BorderColor(0x999).Size(16))
                    .Click(Web.UIClick.Click(new UIClick() { Model = model, Command = "Comment" }.Send("Refer", sub.Id.ToString()))));
                }
            }
            return footer;
        }
        void UIEvent(String key, Subject cmt, bool IsNotComment, String mode)
        {
            var section = UMC.Data.Utility.IntParse(this.AsyncDialog("section", g => this.DialogValue("1")), 0);
            var row = UMC.Data.Utility.IntParse(this.AsyncDialog("row", g => this.DialogValue("1")), 0);
            var ui = this.AsyncDialog("UI", g => this.DialogValue("none"));



            new UISection.Editer(section, row).Put(BSSArea(cmt, mode, this.Context.Request.IsApp, IsNotComment), true).SendTo(this.Context, ui, true);

        }
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var Key = this.AsyncDialog("Key", g =>
             {
                 return new Web.UITextDialog() { Title = "评论的主题" };
             });
            var refer_id = UMC.Data.Utility.UInt64Parse(Key, 0);//.Value;


            var user = this.Context.Token.Identity();
            if (user.IsAuthenticated == false)
            {
                this.Prompt("不支持匿名点赞，请登录", false);
                this.Context.Send("Login", true);

            }

            var auto = this.AsyncDialog("type", g => this.DialogValue("auto"));

            var cmt = DataFactory.Instance().Subject(refer_id);
            Project project = DataFactory.Instance().Project(cmt.project_id.Value);
            var statu = project.Status ?? 0;
            if (auto == "Check")
            {
                UIEvent(Key, cmt, (statu & 2) == 2, request.Model);

            };
            if (DataFactory.Instance().Subject(new Proposal
            {
                ref_id = refer_id,
                user_id = user.Id,
                Type = 1,
                Poster = user.Alias,
                CreationDate = DateTime.Now
            }))
            {
                cmt.Favs = (cmt.Favs ?? 0) + 1;
                UIEvent(Key, cmt, (statu & 2) == 2, request.Model);
            }
            else
            {

                cmt.Favs = (cmt.Favs ?? 0) - 1;
                UIEvent(Key, cmt, (statu & 2) == 2, request.Model);
            }

        }

    }
}